# https://www.notabug.org/Tonypythony/

# conda create -n STOMAAPP ### C:\Users\123\miniconda3\envs
### conda activate STOMAAPP

# pip install matplotlib
# pip install Pillow
# pip install --upgrade cx_Freeze

### conda deactivate

from pathlib import Path
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image #, ImageFont, ImageDraw
from time import time

from pylab import imshow, array, ginput, close # Matplotlib по умолчанию использует формат RGB

root = Tk()

current_dir = Path.cwd() # путь текущей директории
filename = filedialog.askopenfilename(initialdir = current_dir, # initialdir = "/run/media/rick/DATA/Kazahi"
 title = "Select imagefile", filetypes = (("JPG files", "*.JPG"),("jpeg files", "*.jpeg"),
 										("jpg files", "*.jpg"),("png files", "*.png"),
 										("bmp files", "*.bmp"),("all files", "*.*")))

# print(root.winfo_screenwidth())
# print(root.winfo_screenheight())

x_size_screen = int(root.winfo_screenwidth()/3)
y_size_screen = int(root.winfo_screenheight()/3)
# print(x_size_screen)
# print(y_size_screen)


image = Image.open(filename).convert('RGB')#.convert('L')

print(image.size[0],image.size[1],image.size[0]*image.size[1])
s_0 = image.size[0]*image.size[1]
#print(f's_0 = {s_0}')

def color_of_points(img):
	im = array(image)
	imshow(im)
	print('\nPlease click 2 points for crop colors')
	x = ginput(2)
	y = [(int(p), int(q)) for p, q in x]
	a = [y[0][0], y[0][1], y[1][0], y[1][1]]
	#print(a)
	global im_crop
	im_crop = image.crop((y[0][0], y[0][1], y[1][0], y[1][1]))
	imshow(im_crop)

	print('\nPlease click 2 points for detect colors')
	x = ginput(2)
	y = [(int(p), int(q)) for p, q in x]
	#print('You clicked: ', y)

	a = [y[0][0], y[0][1], y[1][0], y[1][1]]
	#print(a)
	clr1 = image.getpixel(y[0])
	clr2 = image.getpixel(y[1])
	close() # закрыть окно с выбором точек
	print('Color 1 point is', clr1) # color of the first selected pixel
	print('Color 2 point is', clr2) # color of the second selected pixel

color_of_points(image)

image = im_crop
#print(image.size[0],image.size[1],image.size[0]*image.size[1])
s_1 = image.size[0]*image.size[1]
k_result = s_1/s_0 # коррекция результата -- компенсация площади из-за отрезанного куска изображения
k_result = round(k_result, 2)
#print(f's_1 = {s_1}, s_0 = {s_0}, k_result = {k_result}')

image = im_crop.resize((x_size_screen,y_size_screen)) #((536,356))
#print(image.size[0],image.size[1],image.size[0]*image.size[1])

x_size_root = x_size_screen + 350
y_size_root = y_size_screen + 370

root.geometry('{}x{}'.format(x_size_root,y_size_root)) #(536,506))

canvas = Canvas(root,width=x_size_screen,height=y_size_screen)

canvas.pack()

pilimage = ImageTk.PhotoImage(image)

imagesprite = canvas.create_image(0,0,image=pilimage, anchor=NW)

container = Frame() # создаём контейнер на главном окне для расположения кнопок и полей ввода
container.pack(side='top', fill='both', expand=True)

redM = 255 # max
redL = 0 # min
greenM = 255 # max
greenL = 0 # min
blueM = 255 # max
blueL = 0 # min

########################################################################

lbl1 = Label(container, text="RedMax = ")  
lbl1.grid(column=6, row=1) 

def get_val_motion1(event):
	global redM
	redM = scal1.get()
	
scal1 = Scale(container, orient=HORIZONTAL, length=int(image.size[0]*0.6), from_=0, to=255,
			tickinterval=20, resolution=1)
scal1.bind("<B1-Motion>", get_val_motion1)
scal1.grid(column=7, row=1)

########################################################################

lbl2 = Label(container, text="RedMin = ")  
lbl2.grid(column=6, row=2) 

def get_val_motion2(event_1):
	global redL
	redL = scal2.get()
	
scal2 = Scale(container, orient=HORIZONTAL, length=int(image.size[0]*0.6), from_=0, to=255,
			tickinterval=20, resolution=1)
scal2.bind("<B1-Motion>", get_val_motion2)
scal2.grid(column=7, row=2)

########################################################################

lbl3 = Label(container, text="GreenMax = ")  
lbl3.grid(column=6, row=3) 

def get_val_motion3(event):
	global greenM
	greenM = sca3.get()
	
sca3 = Scale(container, orient=HORIZONTAL, length=int(image.size[0]*0.6), from_=0, to=255,
			tickinterval=20, resolution=1)
sca3.bind("<B1-Motion>", get_val_motion3)
sca3.grid(column=7, row=3)

########################################################################

lbl4 = Label(container, text="GreenMin = ")  
lbl4.grid(column=6, row=4) 

def get_val_motion4(event_1):
	global greenL
	greenL = scal4.get()
	
scal4 = Scale(container, orient=HORIZONTAL, length=int(image.size[0]*0.6), from_=0, to=255,
			tickinterval=20, resolution=1)
scal4.bind("<B1-Motion>", get_val_motion4)
scal4.grid(column=7, row=4)

########################################################################

lbl5 = Label(container, text="BlueMax = ")  
lbl5.grid(column=6, row=5) 

def get_val_motion5(event):
	global blueM
	blueM = scal5.get()
	
scal5 = Scale(container, orient=HORIZONTAL, length=int(image.size[0]*0.6), from_=0, to=255,
			tickinterval=20, resolution=1)
scal5.bind("<B1-Motion>", get_val_motion5)
scal5.grid(column=7, row=5)

########################################################################

lbl6 = Label(container, text="BlueMin = ")  
lbl6.grid(column=6, row=6) 

def get_val_motion6(event_1):
	global blueL
	blueL = scal6.get()
	
scal6 = Scale(container, orient=HORIZONTAL, length=int(image.size[0]*0.6), from_=0, to=255,
			tickinterval=20, resolution=1)
scal6.bind("<B1-Motion>", get_val_motion6)
scal6.grid(column=7, row=6)

########################################################################

def my_callback3(): # показывает исходную фотографию
	#print('return button pushed')
	global pilimage
	pilimage = ImageTk.PhotoImage(image)
	global imagesprite
	#imagesprite = canvas.create_image(int(image.size[0]*kx_imagesprite),int(image.size[1]*kx_imagesprite),image=pilimage)
	imagesprite = canvas.create_image(0,0,image=pilimage, anchor=NW)

button3 = Button(container , text="Source" , command=my_callback3)
button3.grid(row=3 ,column=0)

########################################################################

def my_callback4(): # сохраняет результат
	file_name = filedialog.asksaveasfilename(initialdir = current_dir,
							filetypes = (("png files", "*.png"),
 										("jpg files", "*.jpg"), 
 										("bmp files", "*.bmp"),("all files", "*.*")), defaultextension="")
	global new_image
	new_image.save(file_name)

button4 = Button(container , text="Save Result" , command=my_callback4)
button4.grid(row=2 ,column=0)

########################################################################

def my_callback6(): # открыть новое маленькое изображение
	global filename
	filename = filedialog.askopenfilename(initialdir = current_dir, # initialdir = "/run/media/rick/DATA/Kazahi"
 				title = "Select imagefile", filetypes = (("JPG files", "*.JPG"),("jpeg files", "*.jpeg"),
 										("jpg files", "*.jpg"),("png files", "*.png"),
 										("bmp files", "*.bmp"),("all files", "*.*")))
	global image
	image = Image.open(filename).convert('RGB')

	color_of_points(image)	

	image = image.resize((x_size_screen,y_size_screen)) #((536,356))
	
	global pilimage
	pilimage = ImageTk.PhotoImage(image)
	global imagesprite
	#imagesprite = canvas.create_image(int(image.size[0]*kx_imagesprite),int(image.size[1]*kx_imagesprite),image=pilimage)
	imagesprite = canvas.create_image(0,0,image=pilimage, anchor=NW)

button6 = Button(container , text="Open New Image" , command=my_callback6)
button6.grid(row=4 ,column=0)

########################################################################

def my_callback7(): # главная функция - для расчёта маленького изображения
	N = 0 # счётчик
	new_im = []
	start = time() # для тестирования скорости работы
	for i in range(image.size[0]):
		for j in range(image.size[1]):
			if (image.getpixel((i,j))[0] >= redL) and (image.getpixel((i,j))[0] <= redM) \
			and (image.getpixel((i,j))[1] >= greenL) and (image.getpixel((i,j))[1] <= greenM) \
			and (image.getpixel((i,j))[2] >= blueL) and (image.getpixel((i,j))[2] <= blueM):
				N += 1
				new_im.append((i,j,image.getpixel((i,j))))
	#global percent
	percent = (N/(image.size[0]*image.size[1])*100)*k_result 

	print("\nResult: {0:.1f}%".format(percent))
	print("RedL = %d; RedM = %d, \nGreenL = %d; GreenM = %d, \nBlueL = %d; \
		BlueM = %d".format(redL, redM, greenL, greenM, blueL, blueM))

	print("Time {0:.3f} s".format(float(round((time()-start)*1e3)/1e3))) # для тестирования скорости работы
	global new_image # нужно для возможности сохранения в дальнейшем
	new_image = Image.new("RGB", (image.size[0], image.size[1]))
	for i in range(len(new_im)):
		new_image.putpixel((new_im[i][0], new_im[i][1]), new_im[i][2])

	
	global pilimage
	pilimage = ImageTk.PhotoImage(new_image)
	global imagesprite
	
	imagesprite = canvas.create_image(0,0,image=pilimage, anchor=NW)

button7 = Button(container , text="Result" , command=my_callback7)
button7.grid(row=1 ,column=0)


root.mainloop()